package com.example.administrator.capital.mine;


import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.capital.R;
import com.example.administrator.capital.adapter.CommentAdapter;
import com.example.administrator.capital.base.BaseBackFragment;
import com.example.administrator.capital.eventbus.TabSelectedEvent;
import com.example.administrator.capital.login.LoginActivity;
import com.example.administrator.capital.mine.second.NewDetailFragment;
import com.example.administrator.capital.model.TestBean;
import com.example.administrator.capital.model.bean.ItemBean;
import com.example.administrator.capital.model.bean.NotifyBean;
import com.example.administrator.capital.presenter.MinePresenter;
import com.example.administrator.capital.presenter.base.IViewRequest;
import com.example.administrator.capital.utils.CommonUtils;
import com.example.administrator.capital.utils.JsonUtils;
import com.example.administrator.capital.utils.PreferenceManager;
import com.example.administrator.capital.utils.ToastUtil;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import eventbusactivityscope.EventBusActivityScope;
import layout.LoadingLayout;
import recycle.BaseViewHolder;

/**
 * A simple {@link Fragment} subclass.
 * 消息列表
 */
public class NewsFragment extends BaseBackFragment {
    @BindView(R.id.recycle_view)
    RecyclerView mRecycleView;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout mSwipeLayout;
    Unbinder unbinder;
    @BindView(R.id.loading)
    LoadingLayout mLoading;
    private TextView mBack, mClear;
    private CommentAdapter<NotifyBean.DataBean> mAdapter;
    private List<TestBean> mTestBeans = new ArrayList<>();
    private MinePresenter mMinePresenter;
    public NewsFragment() {
        // Required empty public constructor
    }
    public static NewsFragment newInstance() {

        Bundle args = new Bundle();
        NewsFragment fragment = new NewsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_news, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mMinePresenter = new MinePresenter(null);
        EventBusActivityScope.getDefault(getActivity()).register(this);
        mLoading.showLoading();
        mClear = view.findViewById(R.id.app_title_action);
        mBack = view.findViewById(R.id.app_title_back);
        mSwipeLayout.setColorSchemeResources(R.color.font_blue);
        mAdapter = new CommentAdapter<NotifyBean.DataBean>(R.layout.news_item_list, null) {
            @Override
            public void setViewData(BaseViewHolder holder, NotifyBean.DataBean item, int position) {
                holder.setText(R.id.title_name, item.getTitle()).setText(R.id.content,item.getContent())
                        .setText(R.id.addtime, CommonUtils.transferLongToDate("yyyy-MM-dd", String.valueOf(item.getAddtime())));
            }

            @Override
            public void setEvent(BaseViewHolder holder, NotifyBean.DataBean item, final int position) {

            }
        };
        mRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        DividerItemDecoration div = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        div.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.custom_divider));
        mRecycleView.addItemDecoration(div);
        mRecycleView.setAdapter(mAdapter);
    }

    @Override
    protected void setListener() {
        super.setListener();
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
        mClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mAdapter.setNewData(null);
                mLoading.setEmptyImage(R.drawable.empty);
                mLoading.showEmpty();
            }
        });
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _mActivity.onBackPressed();

            }
        });
    }
    @Override
    protected void setData() {
        super.setData();
        getData();
    }

    private void getData() {
        mMinePresenter.selectMes(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {
                if (var.equals("请登录")) {
                    mBaseActivity.showActivity(LoginActivity.class,null);
                } else {
                    ToastUtil.showShort(var);
                    mLoading.setEmptyImage(R.drawable.empty);
                    mLoading.showEmpty();
                }

            }
            @Override
            public void onCode(int code) {
                ToastUtil.showShort(code);
                mLoading.showContent();
            }
            @Override
            public void onSuccess(Response<String> data) {
                NotifyBean notifyBean=JsonUtils.parse(data.body(),NotifyBean.class);
                mAdapter.setNewData(notifyBean.getData());
                mLoading.showContent();
                mSwipeLayout.setRefreshing(false);
            }
            @Override
            public void onFail(Response<String> error) {
            }
        }, PreferenceManager.instance().getUseId());
    }
    @Subscribe
    public void onTabSelectedEvent(TabSelectedEvent event) {
       mAdapter.remove(event.position);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
